home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint99s / dos.c < prev    next >
C/C++ Source or Header  |  1992-12-23  |  13KB  |  580 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1992 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. /* miscellaneous DOS functions, and the DOS initialization function */
  8.  
  9. #include "mint.h"
  10.  
  11. #define DOS_MAX 0x140
  12.  
  13. Func dos_tab[DOS_MAX];
  14. short dos_max = DOS_MAX;
  15.  
  16. static void alarmme P_((PROC *));
  17.  
  18. long ARGS_ON_STACK 
  19. s_version()
  20. {
  21.     return Sversion();
  22. }
  23.  
  24. /*
  25.  * Super(new_ssp): change to supervisor mode.
  26.  */
  27.  
  28. long ARGS_ON_STACK
  29. s_uper(new_ssp)
  30.     long new_ssp;
  31. {
  32.     int in_super;
  33.     long r;
  34.  
  35.     TRACE(("Super"));
  36.     in_super = curproc->ctxt[SYSCALL].sr & 0x2000;
  37.  
  38.     if (new_ssp == 1) {
  39.         r = in_super ? -1L : 0;
  40.     }
  41.     else {
  42.         curproc->ctxt[SYSCALL].sr ^= 0x2000;
  43.         r = curproc->ctxt[SYSCALL].ssp;
  44.         if (in_super) {
  45.             if (new_ssp == 0) {
  46.                 DEBUG(("bad Super call"));
  47.                 raise(SIGSYS);
  48.             }
  49.             else {
  50.                 curproc->ctxt[SYSCALL].usp = 
  51.                     curproc->ctxt[SYSCALL].ssp;
  52.                 curproc->ctxt[SYSCALL].ssp = new_ssp;
  53.             }
  54.         }
  55.         else {
  56.             curproc->ctxt[SYSCALL].ssp = 
  57.                 new_ssp ? new_ssp : curproc->ctxt[SYSCALL].usp;
  58.         }
  59.     }
  60.     return r;
  61. }
  62.  
  63. /*
  64.  * get/set time and date functions
  65.  */
  66. long ARGS_ON_STACK t_getdate() { return datestamp; }
  67. long ARGS_ON_STACK t_gettime() { return timestamp; }
  68.  
  69. long ARGS_ON_STACK t_setdate(date)
  70.     int date;
  71. {
  72.     long r = Tsetdate(date);
  73.     datestamp = Tgetdate();
  74.     return r;
  75. }
  76.  
  77. long ARGS_ON_STACK t_settime(time)
  78.     int time;
  79. {
  80.     long r = Tsettime(time);
  81.     timestamp = Tgettime();
  82.     return r;
  83. }
  84.  
  85. /*
  86.  * GEMDOS extension: Syield(): give up the processor if any other
  87.  * processes are waiting. Always returns 0.
  88.  */
  89.  
  90. long ARGS_ON_STACK
  91. s_yield()
  92. {
  93. /* reward the nice process */
  94.     curproc->curpri = curproc->pri;
  95.     sleep(READY_Q, curproc->wait_cond);
  96.     return 0;
  97. }
  98.  
  99. /*
  100.  * GEMDOS extension:
  101.  * Prenice(pid, delta) sets the process priority level for process pid.
  102.  * A "nice" value < 0 increases priority, one > 0 decreases it.
  103.  * Always returns the new priority (so Prenice(pid, 0) queries the current
  104.  * priority).
  105.  *
  106.  * NOTE: for backward compatibility, Pnice(delta) is provided and is equivalent
  107.  * to Prenice(Pgetpid(), delta)
  108.  */
  109.  
  110. long ARGS_ON_STACK
  111. p_renice(pid, delta)
  112.     int pid, delta;
  113. {
  114.     PROC *p;
  115.  
  116.     if (pid <= 0 || 0 == (p = pid2proc(pid))) {
  117.         return EFILNF;
  118.     }
  119.  
  120.     if (curproc->euid && curproc->euid != p->ruid
  121.         && curproc->ruid != p->ruid) {
  122.         DEBUG(("Prenice: process ownership error"));
  123.         return EACCDN;
  124.     }
  125.     p->pri -= delta;
  126.     if (p->pri < MIN_NICE) p->pri = MIN_NICE;
  127.     if (p->pri > MAX_NICE) p->pri = MAX_NICE;
  128.     p->curpri = p->pri;
  129.     return ((long)p->pri) & 0x0ffff;
  130. }
  131.  
  132. long ARGS_ON_STACK
  133. p_nice(delta)
  134.     int delta;
  135. {
  136.     return p_renice(curproc->pid,delta);
  137. }
  138.  
  139. /*
  140.  * GEMDOS extensions: routines for getting/setting process i.d.'s and
  141.  * user i.d.'s
  142.  */
  143.  
  144. long ARGS_ON_STACK p_getpid() { return curproc->pid; }
  145.  
  146. long ARGS_ON_STACK p_getppid() { return curproc->ppid; }
  147.  
  148. long ARGS_ON_STACK p_getpgrp() { return curproc->pgrp; }
  149.  
  150. /* note: Psetpgrp(0, ...) is equivalent to Psetpgrp(Pgetpid(), ...) */
  151. /* also note: Psetpgrp(x, 0) is equivalent to Psetpgrp(x, x) */
  152.  
  153. long ARGS_ON_STACK p_setpgrp(pid, newgrp)
  154.     int pid, newgrp;
  155. {
  156.     PROC *p;
  157.  
  158.     if (pid == 0)
  159.         p = curproc;
  160.     else if (0 == (p = pid2proc(pid)))
  161.         return EFILNF;
  162.     if ( (curproc->euid) && (p->ruid != curproc->ruid)
  163.           && (p->ppid != curproc->pid) )
  164.         return EACCDN;
  165.  
  166.     if (newgrp == 0)
  167.         newgrp = p->pid;
  168.  
  169.     return (p->pgrp = newgrp);
  170. }
  171.  
  172. long ARGS_ON_STACK p_getuid() { return curproc->ruid; }
  173. long ARGS_ON_STACK p_getgid() { return curproc->rgid; }
  174. long ARGS_ON_STACK p_geteuid() { return curproc->euid; }
  175. long ARGS_ON_STACK p_getegid() { return curproc->egid; }
  176.  
  177. long ARGS_ON_STACK
  178. p_setuid(id)
  179.     int id;
  180. {
  181.     if (curproc->euid == 0 || curproc->ruid == id) {
  182.         curproc->ruid = curproc->euid = id;
  183.         return id;
  184.     }
  185.     return EACCDN;
  186. }
  187.  
  188. long ARGS_ON_STACK
  189. p_setgid(id)
  190.     int id;
  191. {
  192.     if (curproc->euid == 0 || curproc->egid == 0 || curproc->rgid == id) {
  193.         curproc->egid = curproc->rgid = id;
  194.         return id;
  195.     }
  196.     return EACCDN;
  197. }
  198.  
  199. /*
  200.  * a way to get/set process-specific user information. the user information
  201.  * longword is set to "arg", unless arg is -1. In any case, the old
  202.  * value of the longword is returned.
  203.  */
  204.  
  205. long ARGS_ON_STACK
  206. p_usrval(arg)
  207.     long arg;
  208. {
  209.     long r;
  210.  
  211.     TRACE(("Pusrval"));
  212.     r = curproc->usrdata;
  213.     if (arg != -1L)
  214.         curproc->usrdata = arg;
  215.     return r;
  216. }
  217.  
  218. /*
  219.  * set the file creation mask to "mode". Returns the old value of the
  220.  * mask.
  221.  */
  222. long ARGS_ON_STACK p_umask(mode)
  223.     unsigned mode;
  224. {
  225.     long oldmask = curproc->umask;
  226.  
  227.     curproc->umask = mode & (~S_IFMT);
  228.     return oldmask;
  229. }
  230.  
  231. /*
  232.  * get/set the domain of a process. domain 0 is the default (TOS) domain.
  233.  * domain 1 is the MiNT domain. for now, domain affects read/write system
  234.  * calls and filename translation.
  235.  */
  236.  
  237. long ARGS_ON_STACK
  238. p_domain(arg)
  239.     int arg;
  240. {
  241.     long r;
  242.     TRACE(("Pdomain(%d)", arg));
  243.  
  244.     r = curproc->domain;
  245.     if (arg >= 0)
  246.         curproc->domain = arg;
  247.     return r;
  248. }
  249.  
  250. /*
  251.  * get process resource usage. 8 longwords are returned, as follows:
  252.  *     r[0] == system time used by process
  253.  *     r[1] == user time used by process
  254.  *     r[2] == system time used by process' children
  255.  *     r[3] == user time used by process' children
  256.  *     r[4] == memory used by process
  257.  *     r[5] - r[7]: reserved for future use
  258.  */
  259.  
  260. long ARGS_ON_STACK
  261. p_rusage(r)
  262.     long *r;
  263. {
  264.     r[0] = curproc->systime;
  265.     r[1] = curproc->usrtime;
  266.     r[2] = curproc->chldstime;
  267.     r[3] = curproc->chldutime;
  268.     r[4] = memused(curproc);
  269.     return 0;
  270. }
  271.  
  272. /*
  273.  * get/set resource limits i to value v. The old limit is always returned;
  274.  * if v == -1, the limit is unchanged, otherwise it is set to v. Possible
  275.  * values for i are:
  276.  *    1:  max. cpu time    (milliseconds)
  277.  *    2:  max. core memory allowed
  278.  *    3:  max. amount of malloc'd memory allowed
  279.  */
  280. long ARGS_ON_STACK
  281. p_setlimit(i, v)
  282.     int i;
  283.     long v;
  284. {
  285.     long oldlimit;
  286.  
  287.     switch(i) {
  288.     case 1:
  289.         oldlimit = curproc->maxcpu;
  290.         if (v >= 0) curproc->maxcpu = v;
  291.         break;
  292.     case 2:
  293.         oldlimit = curproc->maxcore;
  294.         if (v >= 0) {
  295.             curproc->maxcore = v;
  296.             recalc_maxmem(curproc);
  297.         }
  298.         break;
  299.     case 3:
  300.         oldlimit = curproc->maxdata;
  301.         if (v >= 0) {
  302.             curproc->maxdata = v;
  303.             recalc_maxmem(curproc);
  304.         }
  305.         break;
  306.     default:
  307.         DEBUG(("Psetlimit: invalid mode %d", i));
  308.         return EINVFN;
  309.     }
  310.     TRACE(("p_setlimit(%d, %ld): oldlimit = %ld", i, v, oldlimit));
  311.     return oldlimit;
  312. }
  313.  
  314. /*
  315.  * pause: just sleeps on IO_Q, with wait_cond == -1. only a signal will
  316.  * wake us up
  317.  */
  318.  
  319. long ARGS_ON_STACK
  320. p_pause()
  321. {
  322.     TRACE(("Pause"));
  323.     sleep(IO_Q, -1L);
  324.     return 0;
  325. }
  326.  
  327. /*
  328.  * helper function for t_alarm: this will be called when the timer goes
  329.  * off, and raises SIGALRM
  330.  */
  331.  
  332. static void
  333. alarmme(p)
  334.     PROC *p;
  335. {
  336.     p->alarmtim = 0;
  337.     post_sig(p, SIGALRM);
  338. }
  339.  
  340. /*
  341.  * t_alarm(x): set the alarm clock to go off in "x" seconds. returns the
  342.  * old value of the alarm clock
  343.  */
  344.  
  345. long ARGS_ON_STACK
  346. t_alarm(x)
  347.     long x;
  348. {
  349.     long oldalarm;
  350.     TIMEOUT *t;
  351.  
  352. /* see how many milliseconds there were to the alarm timeout */
  353.     oldalarm = 0;
  354.  
  355.     if (curproc->alarmtim) {
  356.         for (t = tlist; t; t = t->next) {
  357.             oldalarm += t->when;
  358.             if (t == curproc->alarmtim)
  359.                 goto foundalarm;
  360.         }
  361.         DEBUG(("Talarm: old alarm not found!"));
  362.         oldalarm = 0;
  363.         curproc->alarmtim = 0;
  364. foundalarm:
  365.         ;
  366.     }
  367.  
  368.     oldalarm = (oldalarm+999) / 1000;    /* convert to seconds */
  369.  
  370. /* we were just querying the alarm */
  371.     if (x < 0)
  372.         return oldalarm;
  373.  
  374. /* cancel old alarm */
  375.     if (curproc->alarmtim)
  376.         canceltimeout(curproc->alarmtim);
  377.  
  378. /* add a new alarm, to occur in 1000*x milliseconds */
  379.     if (x)
  380.         curproc->alarmtim = addtimeout(1000*x, alarmme);
  381.     else
  382.         curproc->alarmtim = 0;
  383.  
  384.     return oldalarm;
  385. }
  386.  
  387. /*
  388.  * sysconf(which): returns information about system configuration.
  389.  * "which" specifies which aspect of the system configuration is to
  390.  * be returned:
  391.  *    -1    max. value of "which" allowed
  392.  *    0    max. number of memory regions per proc
  393.  *    1    max. length of Pexec() execution string {ARG_MAX}
  394.  *    2    max. number of open files per process    {OPEN_MAX}
  395.  *    3    number of supplementary group id's    {currently 0}
  396.  *    4    max. number of processes per uid    {CHILD_MAX}
  397.  *
  398.  * unlimited values (e.g. CHILD_MAX) are returned as 0x7fffffffL
  399.  *
  400.  * See also Dpathconf() in dosdir.c.
  401.  */
  402.  
  403. long ARGS_ON_STACK
  404. s_ysconf(which)
  405.     int which;
  406. {
  407.     if (which == -1)
  408.         return 4;
  409.  
  410.     switch(which) {
  411.         case 0:
  412.             return UNLIMITED;
  413.         case 1:
  414.             return 126;
  415.         case 2:
  416.             return MAX_OPEN;
  417.         case 3:
  418.             return 0;
  419.         case 4:
  420.             return UNLIMITED;
  421.         default:
  422.             return EINVFN;
  423.     }
  424. }
  425.  
  426. /*
  427.  * Salert: send an ALERT message to the user, via the same mechanism
  428.  * the kernel does (i.e. u:\pipe\alert, if it's available
  429.  */
  430.  
  431. long ARGS_ON_STACK
  432. s_alert(str)
  433.     char *str;
  434. {
  435. /* how's this for confusing code? _ALERT tries to format the
  436.  * string as an alert box; if it fails, we let the full-fledged
  437.  * ALERT function (which will try _ALERT, and fail again)
  438.  * print the alert to the debugging device
  439.  */
  440.     if (_ALERT(str) == 0)
  441.         ALERT(str);
  442.     return 0;
  443. }
  444.  
  445. /*
  446.  * routine for initializing DOS
  447.  *
  448.  * NOTE: before adding new functions, check the definition of
  449.  * DOS_MAX at the top of this file to make sure that there
  450.  * is room; if not, increase DOS_MAX.
  451.  */
  452.  
  453. void
  454. init_dos()
  455. {
  456. /* miscellaneous initialization goes here */
  457.     timestamp = Tgettime();
  458.     datestamp = Tgetdate();
  459.  
  460. /* dos table initialization */
  461.     dos_tab[0x00] = p_term0;
  462.     dos_tab[0x01] = c_conin;
  463.     dos_tab[0x02] = c_conout;
  464.     dos_tab[0x03] = c_auxin;
  465.     dos_tab[0x04] = c_auxout;
  466.     dos_tab[0x05] = c_prnout;
  467.     dos_tab[0x06] = c_rawio;
  468.     dos_tab[0x07] = c_rawcin;
  469.     dos_tab[0x08] = c_necin;
  470.     dos_tab[0x09] = c_conws;
  471.     dos_tab[0x0a] = c_conrs;
  472.     dos_tab[0x0b] = c_conis;
  473.     dos_tab[0x0e] = d_setdrv;
  474.     dos_tab[0x10] = c_conos;
  475.     dos_tab[0x11] = c_prnos;
  476.     dos_tab[0x12] = c_auxis;
  477.     dos_tab[0x13] = c_auxos;
  478.     dos_tab[0x14] = m_addalt;
  479.     dos_tab[0x15] = s_realloc;
  480.     dos_tab[0x19] = d_getdrv;
  481.     dos_tab[0x1a] = f_setdta;
  482.     dos_tab[0x20] = s_uper;
  483.     dos_tab[0x2a] = t_getdate;
  484.     dos_tab[0x2b] = t_setdate;
  485.     dos_tab[0x2c] = t_gettime;
  486.     dos_tab[0x2d] = t_settime;
  487.     dos_tab[0x2f] = f_getdta;
  488.     dos_tab[0x30] = s_version;
  489.     dos_tab[0x31] = p_termres;
  490.     dos_tab[0x36] = d_free;
  491.     dos_tab[0x39] = d_create;
  492.     dos_tab[0x3a] = d_delete;
  493.     dos_tab[0x3b] = d_setpath;
  494.     dos_tab[0x3c] = f_create;
  495.     dos_tab[0x3d] = f_open;
  496.     dos_tab[0x3e] = f_close;
  497.     dos_tab[0x3f] = f_read;
  498.     dos_tab[0x40] = f_write;
  499.     dos_tab[0x41] = f_delete;
  500.     dos_tab[0x42] = f_seek;
  501.     dos_tab[0x43] = f_attrib;
  502.     dos_tab[0x44] = m_xalloc;
  503.     dos_tab[0x45] = f_dup;
  504.     dos_tab[0x46] = f_force;
  505.     dos_tab[0x47] = d_getpath;
  506.     dos_tab[0x48] = m_alloc;
  507.     dos_tab[0x49] = m_free;
  508.     dos_tab[0x4a] = m_shrink;
  509.     dos_tab[0x4b] = p_exec;
  510.     dos_tab[0x4c] = p_term;
  511.     dos_tab[0x4e] = f_sfirst;
  512.     dos_tab[0x4f] = f_snext;
  513.     dos_tab[0x56] = f_rename;
  514.     dos_tab[0x57] = f_datime;
  515.     dos_tab[0x5c] = f_lock;
  516.  
  517. /* MiNT extensions to GEMDOS */
  518.  
  519.     dos_tab[0xff] = s_yield;
  520.     dos_tab[0x100] = f_pipe;
  521.     dos_tab[0x104] = f_cntl;
  522.     dos_tab[0x105] = f_instat;
  523.     dos_tab[0x106] = f_outstat;
  524.     dos_tab[0x107] = f_getchar;
  525.     dos_tab[0x108] = f_putchar;
  526.     dos_tab[0x109] = p_wait;
  527.     dos_tab[0x10a] = p_nice;
  528.     dos_tab[0x10b] = p_getpid;
  529.     dos_tab[0x10c] = p_getppid;
  530.     dos_tab[0x10d] = p_getpgrp;
  531.     dos_tab[0x10e] = p_setpgrp;
  532.     dos_tab[0x10f] = p_getuid;
  533.     dos_tab[0x110] = p_setuid;
  534.     dos_tab[0x111] = p_kill;
  535.     dos_tab[0x112] = p_signal;
  536.     dos_tab[0x113] = p_vfork;
  537.     dos_tab[0x114] = p_getgid;
  538.     dos_tab[0x115] = p_setgid;
  539.  
  540.     dos_tab[0x116] = p_sigblock;
  541.     dos_tab[0x117] = p_sigsetmask;
  542.     dos_tab[0x118] = p_usrval;
  543.     dos_tab[0x119] = p_domain;
  544.     dos_tab[0x11a] = p_sigreturn;
  545.     dos_tab[0x11b] = p_fork;
  546.     dos_tab[0x11c] = p_wait3;
  547.     dos_tab[0x11d] = f_select;
  548.     dos_tab[0x11e] = p_rusage;
  549.     dos_tab[0x11f] = p_setlimit;
  550.     dos_tab[0x120] = t_alarm;
  551.     dos_tab[0x121] = p_pause;
  552.     dos_tab[0x122] = s_ysconf;
  553.     dos_tab[0x123] = p_sigpending;
  554.     dos_tab[0x124] = d_pathconf;
  555.     dos_tab[0x125] = p_msg;
  556.     dos_tab[0x126] = f_midipipe;
  557.     dos_tab[0x127] = p_renice;
  558.     dos_tab[0x128] = d_opendir;
  559.     dos_tab[0x129] = d_readdir;
  560.     dos_tab[0x12a] = d_rewind;
  561.     dos_tab[0x12b] = d_closedir;
  562.     dos_tab[0x12c] = f_xattr;
  563.     dos_tab[0x12d] = f_link;
  564.     dos_tab[0x12e] = f_symlink;
  565.     dos_tab[0x12f] = f_readlink;
  566.     dos_tab[0x130] = d_cntl;
  567.     dos_tab[0x131] = f_chown;
  568.     dos_tab[0x132] = f_chmod;
  569.     dos_tab[0x133] = p_umask;
  570.     dos_tab[0x134] = p_semaphore;
  571.     dos_tab[0x135] = d_lock;
  572.     dos_tab[0x136] = p_sigpause;
  573.     dos_tab[0x137] = p_sigaction;
  574.     dos_tab[0x138] = p_geteuid;
  575.     dos_tab[0x139] = p_getegid;
  576.     dos_tab[0x13a] = p_waitpid;
  577.     dos_tab[0x13b] = d_getcwd;
  578.     dos_tab[0x13c] = s_alert;
  579. }
  580.